package com.sigmatrix.h5.web.controller.front;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sigmatrix.h5.auth.ILoginUser;
import com.sigmatrix.h5.config.LoginUser;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.service.common.upload.Base64ImageUploadRequest;
import com.sigmatrix.h5.service.common.upload.UploadService;
import com.sigmatrix.h5.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "上传" })
@RestController
@RequestMapping("/front/upload")
public class UploadController extends BaseController {

	@Autowired
	private UploadService uploadService;


	@ApiOperation(value = "图片上传（base64）", produces = "application/json", consumes = "application/json")
	@PostMapping(value = "/base64Image")
	public List<String> base64IamgeUpload(@LoginUser ILoginUser user,
			@Valid @RequestBody Base64ImageUploadRequest imageUploadRequest) {
		try {
			return uploadService.imageConvertAndUpload(imageUploadRequest, user.getEseCode(),
					"h5-service/image");
		} catch (Throwable e) {
			throw new MessageException("上传失败", e);
		}
	}
	
	@ApiOperation(value = "图片上传（file）",  produces = "application/json", consumes = "multipart/form-data")
	@PostMapping(value = "/file/image")
	public List<String> fileImageUpload(@LoginUser ILoginUser user,
			@RequestParam("file") MultipartFile file) {
		try {
			return uploadService.imageFileUpload(file, user.getEseCode(),
					"h5-service/image");
		} catch (Throwable e) {
			throw new MessageException("上传失败", e);
		}
	}

}
